<template>
  <div class="goodsinfo-container">
    <!-- 小球动画   -->
    <!-- <transition @befor-enter="beforEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>-->
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <!-- 动画的所有方法名称都是固定的 一个字也不能改 不能自己自定义方法的名称 否则会看不到我们想要的效果 -->
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>

    <!-- 商品轮播图区域 -->
    <swiper :lunbotuList="lunbo" :isfull="false"></swiper>

    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goodsinfo.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>¥{{ goodsinfo.market_price }}</del>&nbsp;&nbsp;
            <span class="now_price">销售价：¥{{ goodsinfo.sell_price }}</span>
          </p>
          <p>
            购买数量：
            <numbox @getcount="getSelectCount" :max="goodsinfo.stock_quantity"></numbox>
          </p>
          <mt-button type="primary" size="small">立即购买</mt-button>
          <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
        </div>
      </div>
      <div class="mui-card-footer">页脚</div>
    </div>

    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{ goodsinfo.goods_no }}</p>
          <p>库存情况：{{ goodsinfo.stock_quantity }}件</p>
          <p>上架时间：{{ goodsinfo.add_time | dateFormat }}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComments(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../subcomponents/Swiper.vue";
import numbox from "../subcomponents/GoodsInfo_numbox.vue";
export default {
  data() {
    return {
      lunbo: [], //轮播图列表
      id: this.$route.params.id,
      goodsinfo: {}, //商品参数信息
      ballFlag: false, //小球的显示和隐藏
      SelectCount: 1, //存储购买的数量
    };
  },
  created() {
    this.getLunBu();
    this.getGoodsInfo();
  },
  methods: {
    getLunBu() {
      //获取轮播图
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        if (result.body.status === 0) {
          result.body.message.forEach(item => {
            item.img = item.src;
          });
          this.lunbo = result.body.message;
        }
      });
    },
    getGoodsInfo() {
      //获取商品参数
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        if (result.body.status === 0) {
          this.goodsinfo = result.body.message[0];
        }
      });
    },
    goDesc(id) {
      //获取商品详情
      this.$router.push({ name: "goodsdesc", params: { id } });
    },
    goComments(id) {
      //获取商品评论
      this.$router.push({ name: "goodscomments", params: { id } });
    },
    addToShopCar() {
      //加入到购物车
      this.ballFlag = !this.ballFlag;
      //先拼接处一个当前商品对象
      var goodsInfo = {
        id: this.id,
        count: this.SelectCount,
        price: this.goodsinfo.sell_price,
        selected:true //默认被选中
      };
      this.$store.commit("addToCar", goodsInfo);
    },
    // 动画的所有方法名称都是固定的 一个字也不能改
    //不能自己自定义方法的名称 否则会看不到我们想要的效果
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;
      //获取小球在页面的位置
      const ball_position = this.$refs.ball.getBoundingClientRect();
      //获取购物车徽标在页面的位置
      const badge_position = document
        .getElementById("badge")
        .getBoundingClientRect();

      const X = badge_position.left - ball_position.left;
      const Y = badge_position.top - ball_position.top;

      el.style.transform = `translate(${X}px,${Y}px)`;
      el.style.transition = "all 1s cubic-bezier(.4,-0.3,1,.68)";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectCount(count) {
      //获取购买的数量 子组件通过方法的调用向父组件传值
      this.SelectCount = count;
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>

<style lang="scss" scoped>
.goodsinfo-container {
  .now_price {
    font-size: 16px;
    font-weight: bold;
    color: red;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }

  .ball {
    width: 15px;
    height: 15px;
    background-color: red;
    position: absolute;
    border-radius: 50%;
    top: 356px;
    left: 155px;
    z-index: 99;
  }
}
</style>